#include "filetool.h"
#include "../CheckInfor/DataBase/keywordlib.h"
#include "../CheckInfor/fileinfor.h"
#include "filefilter.h"
#include <QFile>
#include <QFileInfo>
#include <iostream>
#include <QProcess>
#define SPLITPAGE "```"
#define PYTHONIMAGE "../YztyPy/bin/python3 ../YztyPy/image2Text.py"
#define JAVAFILEREAD "../java/jre/bin/java -jar ../java/fileread/fileread.jar"
#define TEMPATH      "../cache/tempfile/"
#define TEMPATHJAVA      "../cache/tempfile/java_"
#define TEMPATHPYTHON    "../cache/tempfile/python_"


FileTool* FileTool::_instance=nullptr;

using namespace Yzty;

FileTool* FileTool::GetInstance()
{
    if(FileTool::_instance==nullptr)
        FileTool::_instance=new FileTool();
    return FileTool::_instance;
}

FileTool::FileTool():_cnts(0)
{

}

FileTool::~FileTool()
{

}

void FileTool::SetFileCheckCondition(const FileCheckCondition &conditon)
{
    this->_condition=conditon;
    this->_hash_word.clear();
    this->_cnts.store(0);
    removecache();                          //清楚缓存
    this->PerProcessionWord(options);       //关键词预处理
}

std::function<void()> FileTool::GetToolFunction(Yzty::FileTypeLimit::Type type,int index)
{
    std::function<void()> func;
    switch(type)
    {
        case FileTypeLimit::DOC:
            func=[this,index](){
                this->File_KeyWord_Check_Doc(index);
            };
            break;
        case FileTypeLimit::RTF:
            func=[this,index](){
                this->File_KeyWord_Check_Rtf(index);
            };
            break;
        case FileTypeLimit::JPEG:
            func=[this,index](){
                this->File_KeyWord_Check_Jpeg(index);
            };
            break;
        case FileTypeLimit::WPT:
            func=[this,index](){
                this->File_KeyWord_Check_Wpt(index);
            };
            break;
        case FileTypeLimit::MHT:
            func=[this,index](){
                this->File_KeyWord_Check_Mht(index);
            };
            break;
        case FileTypeLimit::XLS:
            func=[this,index]{
                this->File_KeyWord_Check_Xls(index);
            };
            break;
        case FileTypeLimit::PPT:
            func=[this,index](){
                this->File_KeyWord_Check_Ppt(index);
            };
            break;
        case FileTypeLimit::HTML:
            func=[this,index](){
                this->File_KeyWord_Check_Html(index);
            };
            break;
        case FileTypeLimit::HTM:
            func=[this,index](){
                this->File_KeyWord_Check_Htm(index);
            };
            break;
        case FileTypeLimit::XML:
            func=[this,index](){
                this->File_KeyWord_Check_Xml(index);
            };
            break;
        case FileTypeLimit::TEXT:
            func=[this,index](){
                this->File_KeyWord_Check_Txt(index);
            };
            break;
        case FileTypeLimit::WPS:
            func=[this,index](){
                this->File_KeyWord_Check_Wps(index);
             };
            break;
        case FileTypeLimit::ET:
            func=[this,index](){
                this->File_KeyWord_Check_Et(index);
             };
            break;
        case FileTypeLimit::DPS:
            func=[this,index](){
                this->File_KeyWord_Check_Dps(index);
             };
            break;
        case FileTypeLimit::DOCX:
            func=[this,index](){
                this->File_KeyWord_Check_Docx(index);
             };
            break;
        case FileTypeLimit::XLSX:
            func=[this,index](){
                this->File_KeyWord_Check_Xlsx(index);
            };
            break;
        case FileTypeLimit::PPTX:
            func=[this,index](){
                this->File_KeyWord_Check_Pptx(index);
            };
            break;
        case FileTypeLimit::PPS:
            func=[this,index](){
                this->File_KeyWord_Check_Pps(index);
            };
            break;
        case FileTypeLimit::PNG:
            func=[this,index](){
                this->File_KeyWord_Check_Png(index);
            };
            break;

        case FileTypeLimit::JPG:
            func=[this,index](){
                this->File_KeyWord_Check_Jpg(index);
            };
            break;

        case FileTypeLimit::BMP:
            func=[this,index](){
                this->File_KeyWord_Check_Bmp(index);
            };
            break;

        case FileTypeLimit::PDF:
            func=[this,index](){
                this->File_KeyWord_Check_Pdf(index);
            };
            break;
        case FileTypeLimit::DOT:
            func=[this,index](){
                this->File_KeyWord_Check_Dot(index);
            };
            break;
        case FileTypeLimit::DOTX:
            func=[this,index](){
                this->File_KeyWord_Check_Dotx(index);
            };
            break;
        case FileTypeLimit::ODT:
            func=[this,index](){
                this->File_KeyWord_Check_Odt(index);
            };
            break;
        case FileTypeLimit::ODS:
            func=[this,index](){
                this->File_KeyWord_Check_Ods(index);
            };
            break;
        case FileTypeLimit::ODP:
            func=[this,index](){
                this->File_KeyWord_Check_Odp(index);
            };
            break;

        default:
            break;
    }
    return func;
}

QVector<FileKeyWordPos> FileTool::File_KeyWord_Check_Interface(const QString& filename,const QMap<QString,int>& options,QString &subStrings)
{
    QVector<FileKeyWordPos> result;
    QFile file(filename);
    if(!file.open(QIODevice::ReadOnly))
        return result;
    QString name=file.fileName();
    QStringList text=QString(file.readAll()).split(SPLITPAGE);
    for(int i=0;i<text.size();++i)
    {
        if(options.count("bkhash")==1)
        {
            QVector<FileKeyWordPos>&& temp=BkdrHashFunction_CheckMenthod(text[i].toLocal8Bit());
            for(FileKeyWordPos& pos:temp)
                pos._page=uint16_t(i)+1;
            result.append(temp);
            if(temp.size()!=0){
                subStrings.append(text[i]);
                subStrings.append("\n");
            }
        }
    }
    return result;
}

QVector<FileKeyWordPos> FileTool::BkdrHashFunction_CheckMenthod(const QString& text)
{
    QVector<FileKeyWordPos> result;
    QVector<quint64> P(text.size()+1),PerWord(text.size()+1);
    P[0]=1;
    PerWord[0]=0;
    for(int i=1;i<=text.size();++i)
    {
        P[i]=P[i-1]*FileTool::bkhash;
        PerWord[i]=PerWord[i-1]*FileTool::bkhash+quint64(text[i-1].unicode());
    }

    int col=0,row=1;
    for(int i=1;i<=text.size();++i)
    {
        if(text[i-1]=='\n')
        {
            row++;
            col=1;
            continue;
        }

        for(int len:_lengths)   //遍历所有存在的关键词长度
        {
            if(i>=len)
            {
                quint64 key=PerWord[i]-PerWord[i-len]*P[len];
                if(_hash_word.count(key))
                {
                    for(auto temp:_hash_word[key])
                    {
                        FileKeyWordPos pos;
                        pos._col=uint16_t(col-len+1)+1;
                        pos._row=uint16_t(row);
                        pos._lib_id=temp.first;
                        pos._keyword_id=uint32_t(temp.second);
                        result.push_back(pos);
                    }
                }
            }
        }
        col++;
        /*
         * 行列处理存在偏差
         * 关键词存在换行时，匹配存在问题　可以在预处理时进行解决
         * datetime:2021/12/28 02:00pm
        */
    }
    return result;
}

void FileTool::PerProcessionWord(const QMap<QString,int>& options)
{
    /*
     * 将所有单词hash
    */
    WordLibCode marks=_condition._keywordlimit._marks;
    for(int i=0;i<63;++i)
    {
        if((marks>>i)&1)
        {
            const QVector<QString>& words=KeyWordLib::GetInstance()->GetWordList(int64_t(1)<<i);

            for(int j=0;j<words.size();++j)
            {
                QString word=words[j];
                _lengths.insert(word.size());
                quint64 key=0;
                std::cout<<word.toStdString()<<std::endl;
                if(options["bkhash"]==1)
                {
                    for(QChar c:word)
                        key=key*FileTool::bkhash+quint64(c.unicode());
                }

                _hash_word[key].push_back(QPair<WordLibCode,int>(int64_t(1)<<i,j));
            }

        }
    }
}

void FileTool::File_KeyWord_Check_Doc(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::DOC)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::DOC).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);
    record->_type=FileTypeLimit::DOC;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;
    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Rtf(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::RTF)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::RTF).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::RTF;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Dot(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::DOT)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::DOT).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::DOT;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Xls(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::XLS)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::XLS).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::XLS;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Ppt(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::PPT)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::PPT).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::PPT;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Html(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::HTML)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::HTML).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::HTML;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}
void FileTool::File_KeyWord_Check_Htm(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::HTM)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::HTM).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::HTM;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}
void FileTool::File_KeyWord_Check_Mht(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::MHT)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::MHT).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::MHT;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Xml(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::XML)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::XML).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::XML;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Txt(int index)
{
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::TEXT)[index];

    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(file.filePath(),options,record->_hitSubstrings);


    QVector<FileKeyWordPos>&& nameflag=BkdrHashFunction_CheckMenthod(file.fileName().toLocal8Bit());
    if(nameflag.size()>0)
        record->_filename_flag=true;

    record->_type=FileTypeLimit::TEXT;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Wps(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::WPS)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::WPS).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::WPS;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}
void FileTool::File_KeyWord_Check_Wpt(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::WPT)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::WPT).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::WPT;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}


void FileTool::File_KeyWord_Check_Et(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::ET)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::ET).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::ET;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Dps(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::DPS)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::DPS).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::DPS;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Docx(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::DOCX)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::DOCX).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::DOCX;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}
void FileTool::File_KeyWord_Check_Dotx(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::DOTX)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::DOTX).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::DOTX;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}
void FileTool::File_KeyWord_Check_Xlsx(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::XLSX)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::XLSX).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::XLSX;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Pptx(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::PPTX)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::PPTX).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::PPTX;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}
void FileTool::File_KeyWord_Check_Pps(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::PPS)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::PPS).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::PPS;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Png(int index){
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::PNG)[index];
    QString tempath=TEMPATHPYTHON+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(PYTHONIMAGE).arg(strpath).arg(FileTypeLimit::PNG).arg(tempath);
    system(command.toLocal8Bit());      //执行外部python文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::PTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::PNG;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Jpg(int index){
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::JPG)[index];
    QString tempath=TEMPATHPYTHON+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(PYTHONIMAGE).arg(strpath).arg(FileTypeLimit::JPG).arg(tempath);
    system(command.toLocal8Bit());      //执行外部python文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::PTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::JPG;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}
void FileTool::File_KeyWord_Check_Jpeg(int index){
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::JPEG)[index];
    QString tempath=TEMPATHPYTHON+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(PYTHONIMAGE).arg(strpath).arg(FileTypeLimit::JPEG).arg(tempath);
    system(command.toLocal8Bit());      //执行外部python文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::PTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::JPEG;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Bmp(int index){
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::BMP)[index];
    QString tempath=TEMPATHPYTHON+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(PYTHONIMAGE).arg(strpath).arg(FileTypeLimit::BMP).arg(tempath);
    system(command.toLocal8Bit());      //执行外部python文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::PTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::BMP;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);

}

void FileTool::File_KeyWord_Check_Pdf(int index){
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::PDF)[index];
    QString tempath=TEMPATHPYTHON+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    //    QString command=QString("%1 %2 %3 %4").arg(PYTHONIMAGE).arg(file.filePath()).arg(FileTypeLimit::PDF).arg(tempath);
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::PDF).arg(tempath);
//    system(command.toLocal8Bit());      //执行外部python文件
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::PDF;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}


void FileTool::File_KeyWord_Check_Odt(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::ODT)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::ODT).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::ODT;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}



void FileTool::File_KeyWord_Check_Ods(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::ODS)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::ODS).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::ODS;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::File_KeyWord_Check_Odp(int index)
{
    int pi=_cnts++;
    const QFileInfo& file=FileFilter::GetInstance()->GetSelectAblePaths(FileTypeLimit::ODP)[index];
    QString tempath=TEMPATHJAVA+QString::number(pi)+".txt";
    QString strpath=this->replacestr(file.filePath());
    QString command=QString("%1 %2 %3 %4").arg(JAVAFILEREAD).arg(strpath).arg(FileTypeLimit::ODP).arg(tempath);
    system(command.toLocal8Bit());      //执行外部java文件
    ignoreSpace(tempath);
    FileCheckRecord* record=new FileCheckRecord(FileToolCode::FTOTAL);
    QVector<FileKeyWordPos>&& temp=File_KeyWord_Check_Interface(tempath,options,record->_hitSubstrings);

    record->_type=FileTypeLimit::ODP;
    record->_file_id=uint32_t(index);
    record->_key_pos=temp;

    //存入消息缓存
    FileInfor::GetInstance()->AddFileCheckRecord(record);
}

void FileTool::SetOption(QString item, int value)
{
    this->options[item]=value;
}

void FileTool::removecache()
{
    QVector<QString> temp;
    QDir dir(TEMPATH);
    for(QFileInfo path:dir.entryInfoList()){
        temp.push_back(path.filePath());
    }
    for(QString t:temp)
        QFile::remove(t);
}


void  FileTool::ignoreSpace(QString &filename){
    QFile file(filename);
    QString str;
    if(file.open(QIODevice::ReadOnly|QIODevice::Text)){
        str=file.readAll();
        str.remove(' ');
        file.close();
    }
    if(file.open(QIODevice::WriteOnly|QIODevice::Text)){
        QTextStream in(&file);
        in<<str<<endl;
        file.close();
    }
}


QString FileTool::replacestr(QString &&s){
    QString str(s);
//    str.replace(QRegExp(" "),QString("\\ "));
//    str.replace(QRegExp(" "),QString("\\ "));
    str.replace(QRegExp("("),QString("'('"));
    str.replace(QRegExp(")"),QString("')'"));  //替换英文括号
    str.replace(QRegExp("（"),QString("'（'"));
    str.replace(QRegExp("）"),QString("'）'"));//替换中文括号
    str="\'"+str+"\'";
    return str;
}
